<template>
  <div>
    <a-tree
      :show-line="true"
      v-model:expandedKeys="expandedKeys"
      v-model:selectedKeys="selectedKeys"
      multiple
      v-model:checkedKeys="checkedKeys"
      checkable
      :tree-data="treeData"
      draggable
      @dragenter="onDragEnter"
      @drop="onDrop"
      @expand="handleExpand"
    >

      <template #title="{ key: treeKey, title }">
        <a-dropdown :trigger="['contextmenu']">
          <span>{{ title }}</span>
          <template #overlay>
            <a-menu @click="({ key: menuKey }) => onContextMenuClick(treeKey, menuKey)">
              <a-menu-item key="1">1st menu item</a-menu-item>
              <a-menu-item key="2">2nd menu item</a-menu-item>
              <a-menu-item key="3">3rd menu item</a-menu-item>
            </a-menu>
          </template>
        </a-dropdown>
      </template>
    </a-tree>
  </div>
</template>

<script>
import difference from 'lodash-es/difference';
export default {
  name: '',
  components: {},
  data() {
    return {
      treeData: [
        {
          title: 'parent 1',
          key: '0-0',
          children: [
            {
              title: 'parent 1-0',
              key: '0-0-0',
              // disabled: true,
              children: [
                {
                  title: 'leaf',
                  key: '0-0-0-0',
                  // disableCheckbox: true,
                },
                {
                  title: 'leaf',
                  key: '0-0-0-1',
                },
              ],
            },
            {
              title: 'parent 1-1',
              key: '0-0-1',
              children: [
                {
                  key: '0-0-1-0',
                  title: 'sss',
                },
              ],
            },
          ],
        },
      ],
      expandedKeys: ['0-0-0', '0-0-1'],
      selectedKeys: ['0-0-0', '0-0-1'],// 是干啥的
      checkedKeys: ['0-0-0', '0-0-1'],
    }
  },
  created() { },
  mounted() { },
  methods: {
    onDrop(info) {
      console.log(info);
      const dropKey = info.node.key;
      const dragKey = info.dragNode.key;
      const dropPos = info.node.pos.split('-');
      const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
      const loop = (data, key, callback) => {
        data.forEach((item, index) => {
          if (item.key === key) {
            return callback(item, index, data);
          }
          if (item.children) {
            return loop(item.children, key, callback);
          }
        });
      };
      const data = [...this.treeData];

      // Find dragObject
      let dragObj;
      loop(data, dragKey, (item, index, arr) => {
        arr.splice(index, 1);
        dragObj = item;
      });
      if (!info.dropToGap) {
        // Drop on the content
        loop(data, dropKey, item => {
          item.children = item.children || [];
          /// where to insert 示例添加到头部，可以是随意位置
          item.children.unshift(dragObj);
        });
      } else if (
        (info.node.children || []).length > 0 &&
        // Has children
        info.node.expanded &&
        // Is expanded
        dropPosition === 1 // On the bottom gap
      ) {
        loop(data, dropKey, item => {
          item.children = item.children || [];
          // where to insert 示例添加到头部，可以是随意位置
          item.children.unshift(dragObj);
        });
      } else {
        let ar = [];
        let i = 0;
        loop(data, dropKey, (_item, index, arr) => {
          ar = arr;
          i = index;
        });
        if (dropPosition === -1) {
          ar.splice(i, 0, dragObj);
        } else {
          ar.splice(i + 1, 0, dragObj);
        }
      }
      this.treeData = data;
    },
    onDragEnter() { },
    onContextMenuClick(treeKey, menuKey) {
      console.log(`treeKey: ${treeKey}, menuKey: ${menuKey}`);
    },
    handleExpand(keys, { expanded, node }) {
      // node.parent add from 3.0.0-alpha.10
      const tempKeys = ((node.parent ? node.parent.children : this.treeData) || []).map(({ key }) => key);
      if (expanded) {
        this.expandedKeys = difference(keys, tempKeys).concat(node.key);
      } else {
        this.expandedKeys = keys;
      }
    },
  },
  computed: {}
}
</script>
<style scoped lang='scss'>
</style>
